package com.hs.android.fortress.intro;

import com.google.gson.Gson;
import com.hs.android.fortress.GameApplication;
import com.hs.android.fortress.R;
import com.hs.android.fortress.model.Player;
import com.hs.android.fortress.model.RoomInformation;
import com.hs.android.fortress.net.AsyncNetworking;
import com.hs.android.fortress.net.FormPostCommand;
import com.hs.android.fortress.net.ParamsMaker;
import com.hs.android.fortress.net.AsyncNetworking.onNetworkProcessListener;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class loginFragment extends Fragment{

	private TextView id, pw;
	private EditText edtId, edtPw;
	private Button btnLogin, btnSignup;
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_login, container, false);
		
		initialize(v);
		
		btnLogin.setOnClickListener(clickListener);
		btnSignup.setOnClickListener(clickListener);
		
		return v;
	}
	
	private void initialize(View v) {

		id = (TextView) v.findViewById(R.id.id);
		pw = (TextView) v.findViewById(R.id.pw);
		edtId = (EditText) v.findViewById(R.id.edt_id);
		edtPw = (EditText) v.findViewById(R.id.edt_password);
		btnLogin = (Button) v.findViewById(R.id.btn_login);
		btnSignup = (Button) v.findViewById(R.id.btn_signup);

	}
	
	private OnClickListener clickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_login:
				ConnectNetwork("/member/login", "Fail : Wrong id/pw");
				break;

			case R.id.btn_signup:
				ConnectNetwork("/member/join", "Fail : id existed already");
				break;
			}
		}
	};
	
	private void ConnectNetwork(String url, final String errorMessage){
		final String id = edtId.getText().toString();
		final String pw = edtPw.getText().toString();
		
		if(id != null && pw != null){
			//new AsyncNetworking(getActivity(), url, AsyncNetworking.MIME_JSON, params, 
			AsyncNetworking.newInstance(getActivity(), new FormPostCommand(url,
					ParamsMaker.makeIdPwParams(id, pw)), 
					new onNetworkProcessListener() {
						
						@Override
						public void onPostExecute(String result) {
							if(result == null){
								AsyncNetworking.makeNetworkProblemDialog(getActivity())
								.setPositiveButton("OK", null)
								.create().show();
								return;
							}else if(result.equals("wrong")|| result.equals("exist")){
								new AlertDialog.Builder(getActivity())
								.setPositiveButton("OK", null)
								.setMessage(errorMessage)
								.create().show();
							}else{								
								GameApplication app = (GameApplication) getActivity()
										.getApplication();
								app.setMyPlayer(JSONToPlayer(result));
								
								SharedPreferences pref = getActivity().getSharedPreferences("client", getActivity().MODE_PRIVATE);
								Editor editor = pref.edit();
								editor.putString("id", id);
								editor.putString("pw", pw);
								editor.commit();
								
								Intent intent = new Intent(getActivity(), MenuActivity.class);
								startActivity(intent);
								getActivity().finish();
							}
						}
					}).execute();
			
		}else{
			new AlertDialog.Builder(getActivity())
			.setPositiveButton("OK", null)
			.setMessage("ID/Password in not inserted")
			.create().show();
			return;
		}
	}
	
	
	private Player JSONToPlayer(String json) {
		Gson gson = new Gson();		 
		Player player = gson.fromJson(json, Player.class);		
		return player; 
	}
}
